package com.djinfo.work.controller;

import com.djinfo.work.common.bean.AjaxJson;
import com.djinfo.work.common.util.CommonUtil;
import com.djinfo.work.common.util.StringUtil;
import com.djinfo.work.entity.*;
import com.djinfo.work.service.*;
import com.djinfo.work.util.GsonUtil;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by gz on 2017/6/8.
 */
@Controller
@RequestMapping("/qsuser")
public class QsUserController {

    @Autowired
    private QsUserService qsUserService;

    @Autowired
    private QsResultService qsResultService;
    @Autowired
    private QsUserQuestionService qsUserQuestionService;
    @Autowired
    private QsTitleService qsTitleService;
    @Autowired
    private QsContentService qsContentService;


    @ResponseBody
    @RequestMapping("/save2.do")
    public AjaxJson save(QsUserEntity qs, HttpServletRequest request){
        String id = String.valueOf(request.getSession().getAttribute("userId"));
        AjaxJson ajaxJson = new AjaxJson();
        try{

            qsUserService.saveOrUpdate(qs,id);
            ajaxJson.setCode(101);
            ajaxJson.setMsg("success");
        }catch (Exception e){
            ajaxJson.setCode(201);
            ajaxJson.setMsg("error");
            e.printStackTrace();
        }
        return ajaxJson;
    }

    /**
     * 列表
     */
    @RequestMapping("/list.do")
    @ResponseBody
    public AjaxJson list(QsUserEntity qs, Integer pagenum, Integer pagesize){
        HashMap<String, Object> map = new HashMap<>();
        AjaxJson ajaxJson = new AjaxJson();
        try{
            List<QsUserEntity> res = qsUserService.page(qs,pagenum,pagesize);
            Integer count = qsUserService.getAllRowCount(qs);
            map.put("dataList",res);
            map.put("total",count);
            ajaxJson.setCode(101);
            ajaxJson.setMsg("success");
            ajaxJson.setResult(map);
        }catch (Exception e){
            ajaxJson.setCode(201);
            ajaxJson.setMsg("error");
        }
        return ajaxJson;
    }

    /**
     * 获取对象信息
     */
    @RequestMapping("/info.do")
    public ModelAndView info(QsUserEntity qs){
        HashMap<String, Object> map = new HashMap<>();
        AjaxJson ajaxJson = new AjaxJson();
        try{
            QsUserEntity res = qsUserService.selectOne(qs);
            map.put("data",res);
            ajaxJson.setCode(101);
            ajaxJson.setMsg("success");
            ajaxJson.setResult(map);
        }catch (Exception e){
            ajaxJson.setCode(201);
            ajaxJson.setMsg("error");
        }
        return new ModelAndView("qs_user/edit.html","map",map);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete.do")
    @ResponseBody
    public AjaxJson delete(QsUserEntity qs){
        AjaxJson ajaxJson = new AjaxJson();
        try{
            qsUserService.delete(qs);
            ajaxJson.setCode(101);
            ajaxJson.setMsg("success");
        }catch (Exception e){
            ajaxJson.setCode(201);
            ajaxJson.setMsg("error");
        }
        return ajaxJson;
    }

   //曙光 项目 ————————————————————————————————————————————————————————

    @ResponseBody
    @RequestMapping("/save.do")
    public AjaxJson save(QsUserEntity qs){
        AjaxJson ajaxJson=qsUserService.saveQsUser(qs);
        return ajaxJson;
    }

    @ResponseBody
    @RequestMapping("/checkUsername.do")
    public AjaxJson checkUsername(QsUserEntity qs){
        AjaxJson ajaxJson=qsUserService.checkUsername(qs.getpName());
        return ajaxJson;
    }

    @ResponseBody
    @RequestMapping("/login.do")
    public AjaxJson login(QsUserEntity qsUserEntity,HttpServletRequest request){
        AjaxJson ajaxJson=new AjaxJson();
        QsUserEntity qsUserEntity1=qsUserService.login(qsUserEntity);
        if(qsUserEntity1==null){
            ajaxJson.setCode(201);
            return ajaxJson;
        }
        request.getSession().setAttribute("userId",qsUserEntity.getId());
        return ajaxJson;
    }


    /**
     * 保存答案
     * @param jsonStr
     * @return
     */
    @RequestMapping("/saveQsResult.do")
    @ResponseBody
    public AjaxJson saveQsResult(String jsonStr, HttpServletRequest request){
        AjaxJson ajaxJson=new AjaxJson();

        String qId="402881cd5fb857d3015fb86e10e50004";

        String userId=(String)request.getSession().getAttribute("userId");
        QsUserQuestionEntity qsUserQuestionEntity=new QsUserQuestionEntity();
        qsUserQuestionEntity.setuId(userId);
        qsUserQuestionEntity.setCreateTime(StringUtil.getNowTime());
        qsUserQuestionEntity.setUpdateTime(StringUtil.getNowTime());
        qsUserQuestionService.save(qsUserQuestionEntity);


        List list=new ArrayList();
        String nowTime= StringUtil.getNowTime();
        Map<String,QsResultEntity> map = GsonUtil.toCollection(jsonStr,new TypeToken<Map<String,QsResultEntity>>(){});
        Set set = map.keySet(); //key装到set中
        Iterator it = set.iterator(); //返回set的迭代器 装的key值
        while (it.hasNext()){
            String qsResultId = (String)it.next();
            QsResultEntity qsResultEntity = (QsResultEntity)map.get(qsResultId);
            qsResultEntity.setCreateTime(nowTime);
            qsResultEntity.setUpdateTime(nowTime);
            qsResultEntity.setUserId(userId);
            qsResultEntity.setQsId(qsUserQuestionEntity.getId());
            qsResultEntity.setQuestionId(qId);
            qsResultEntity.setIsAttached("0");
            list.add(qsResultEntity);
        }
        qsResultService.saveList(list);

        return ajaxJson;
    }


    /**
     * 获取问卷
     */
    @RequestMapping("/info2.do")
    @ResponseBody
    public AjaxJson info(QsQuestionEntity qs){

        HashMap<String, Object> mapRes = new HashMap<>();
        AjaxJson ajaxJson = new AjaxJson();

        String uuid= CommonUtil.UUID();

        try{
            String qsId = "402881cd5fb857d3015fb86e10e50004";

            List<QsTitleEntity> level2List;
            List<QsTitleEntity> level3List;
            List<QsTitleEntity> level4List;
            List<QsTitleEntity> level5List;
            List<QsContentEntity> content1List;
            List<QsContentEntity> content2List;
            List<QsContentEntity> content3List;
            List<QsContentEntity> content4List;
            List<QsContentEntity> content5List;

            List list1=new ArrayList();
            //获取所有的一级标题
            List<QsTitleEntity> level1List=qsTitleService.getTitleListByLevel(qsId,"1");
            //一级标题下面的内容
            List<QsContentEntity> contentList1=new ArrayList();
            //二级标题对应的内容
            List<QsContentEntity> contentList2=new ArrayList();
            //三级标题对应的内容
            List<QsContentEntity> contentList3=new ArrayList();
            //四级标题对应的内容
            List<QsContentEntity> contentList4=new ArrayList();
            //五级标题对应的内容
            List<QsContentEntity> contentList5=new ArrayList();



            for(QsTitleEntity qsTitleEntity:level1List){
                List list2=new ArrayList();
                Map map1=new HashMap();
                if("1".equals(qsTitleEntity.getIsNoChild())){
                    level2List=qsTitleService.getTitleListById(qsTitleEntity.getId());
                    for(QsTitleEntity qsTitleEntity2:level2List){//二级
                        List list3=new ArrayList();
                        Map map2=new HashMap();
                        if("1".equals(qsTitleEntity2.getIsNoChild())){
                            level3List=qsTitleService.getTitleListById(qsTitleEntity2.getId());
                            for(QsTitleEntity qsTitleEntity3:level3List){//三级
                                List list4=new ArrayList();
                                content3List=new ArrayList<>();
                                Map map3=new HashMap();
                                if("1".equals(qsTitleEntity3.getIsNoChild())){
                                    level4List =qsTitleService.getTitleListById(qsTitleEntity3.getId());
                                    for(QsTitleEntity qsTitleEntity4:level4List){
                                        List list5=new ArrayList();
                                        Map map4=new HashMap();
                                        if("1".equals(qsTitleEntity4.getIsNoChild())){
                                            level5List=qsTitleService.getTitleListById(qsTitleEntity4.getId());
                                            map4.put("titleList",list5);
                                            map4.put("qsTitleEntity",qsTitleEntity4);
                                            for(QsTitleEntity qsTitleEntity5:level5List){
                                                Map map5=new HashMap();
                                                map5.put("qsTitleEntity",qsTitleEntity5);
                                                content5List=qsContentService.getByTitleId(qsTitleEntity4.getId());
                                                for(QsContentEntity qsContentEntity:content5List){
                                                    QsResultEntity qsResultEntity=qsResultService.getQsresultEntityByUidAndContentId(uuid,qsContentEntity.getId());
                                                    qsContentEntity.setQsResultEntity(qsResultEntity);
                                                    contentList5.add(qsContentEntity);
                                                }
                                                map5.put("contentList",contentList5);
                                                list5.add(map5);
                                            }
                                        }else{
                                            content4List=qsContentService.getByTitleId(qsTitleEntity4.getId());
                                            map4.put("qsTitleEntity",qsTitleEntity4);

                                            for(QsContentEntity qsContentEntity:content4List){
                                                QsResultEntity qsResultEntity=qsResultService.getQsresultEntityByUidAndContentId(uuid,qsContentEntity.getId());
                                                qsContentEntity.setQsResultEntity(qsResultEntity);
                                                contentList4.add(qsContentEntity);
                                            }

                                            map4.put("contentList",contentList4);

                                        }
                                        list4.add(map4);
                                    }
                                    map3.put("qsTitleEntity",qsTitleEntity3);
                                    map3.put("titleList",list4);
                                }else{
                                    map3.put("qsTitleEntity",qsTitleEntity3);
                                    content3List=qsContentService.getByTitleId(qsTitleEntity3.getId());
                                    map3.put("contentList",content3List);
                                }
                                list3.add(map3);
                            }
                            map2.put("qsTitleEntity",qsTitleEntity2);
                            map2.put("titleList",list3);
                        }else {
                            map2.put("qsTitleEntity",qsTitleEntity2);
                            content2List=qsContentService.getByTitleId(qsTitleEntity2.getId());
                            map2.put("contentList",content2List);
                        }
                        list2.add(map2);
                    }
                    map1.put("qsTitleEntity",qsTitleEntity);
                    map1.put("titleList",list2);
                }else{
                    map1.put("qsTitleEntity",qsTitleEntity);
                    content1List=qsContentService.getByTitleId(qsTitleEntity.getId());
                    map1.put("contentList",content1List);
                }
                list1.add(map1);
            }
            mapRes.put("titleList",list1);
            ajaxJson.setResult(mapRes);
        }catch (Exception e){
            ajaxJson.setCode(201);
            ajaxJson.setMsg("error");
        }
        return ajaxJson;
    }






}
